package com.cn.boot.student.service.rbac.impl;

import com.cn.boot.student.service.rbac.IRolePermissionService;
import com.cn.boot.student.entity.PermissionEntity;
import com.cn.boot.student.entity.RolePermissionRelationEntity;
import com.cn.boot.student.mapper.RolePermissionMapper;
import com.cn.boot.student.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色权限管理服务逻辑层
 */
@Service
public class RolePermissionServiceImpl implements IRolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    /**
     * 通过角色id得到该角色所拥有的权限信息
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult getIsPermissionByRoleId(Integer id) {
        List<PermissionEntity> list = rolePermissionMapper.getIsPermissionByRoleId(id);
        if(list.size() == 0){
            return CommonResult.failed("暂无数据，请分配权限!!!");
        }
        return CommonResult.success(list);
    }

    /**
     * 通过角色id得到该角色没有的权限信息
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult getNoPermissionByRoleId(Integer id) {
        List<PermissionEntity> list = rolePermissionMapper.getNoPermissionByRoleId(id);
        if(list.size() == 0){
            return CommonResult.failed("暂无数据，请添加权限!!!");
        }
        return CommonResult.success(list);
    }

    /**
     * 通过角色id,权限名得到该角色拥有的权限信息
     *
     * @param entity
     * @return
     */
    @Override
    public CommonResult getIsPermissionByRoleIdAndPermissionName(RolePermissionRelationEntity entity) {
        List<RolePermissionRelationEntity> list = rolePermissionMapper.getIsPermissionByRoleIdAndPermissionName(entity);
        if(list.size() == 0){
            return CommonResult.failed("暂无数据，请分配权限!!!");
        }
        return CommonResult.success(list);
    }

    /**
     * 通过角色id,权限名得到该角色拥有的权限信息
     *
     * @param entity
     * @return
     */
    @Override
    public CommonResult getNoPermissionByRoleIdAndPermissionName(RolePermissionRelationEntity entity) {
        List<RolePermissionRelationEntity> list = rolePermissionMapper.getNoPermissionByRoleIdAndPermissionName(entity);
        if(list.size() == 0){
            return CommonResult.failed("暂无数据，请添加权限!!!");
        }
        return CommonResult.success(list);
    }

    /**
     * 添加权限
     *
     * @param entities
     * @return
     */
    @Override
    public CommonResult adds(List<RolePermissionRelationEntity> entities) {
        entities.stream().forEach(rolePermissionMapper::add);
        return CommonResult.success(entities.size(),"成功分配了"+entities.size()+"条权限");
    }

    /**
     * 删除权限
     *
     * @param entities
     * @return
     */
    @Override
    public CommonResult dels(List<RolePermissionRelationEntity> entities) {
        entities.stream().forEach(rolePermissionMapper::del);
        return CommonResult.success(entities.size(),"成功删除了"+entities.size()+"条权限");
    }
}
